<?php
namespace app\common\controller;
use app\common\model\CollegeModel;
use think\Controller;
use think\Container;
use think\Db;

class BaseController extends Controller
{
    public function __construct()
    {
        $this->_initializeView();
        parent::__construct();

        $this->view  = Container::get('view')->init(
            $this->app['config']->pull('template')
        );

        if (session('uid')){
            $user = json_decode(session('userInfo'),true);
            $this->assign('uid',session('uid'));
            $this->assign('user',$user);
        }else{
            $this->assign('uid',null);
            $this->assign('user',null);
        }

        $collegeList = CollegeModel::where('status',1)->order('sort','desc')->all();
        $this->assign('allColleges',$collegeList);
    }

    public function initialize() {
        parent::initialize();
        $site_info = get_option('site_info');
        $links = get_links();
        $navs = get_navs();

        $this->assign(['site_info'=>$site_info,'navs'=>$navs,'links'=>$links]);
    }

    // 初始化
    protected function _initializeView()
    {
        $themeBase = 'template/';

        $frontDefaultTheme = 'frontend';

        $themePath = "{$themeBase}{$frontDefaultTheme}/";

        config ( 'template.view_base', $themePath);

        $tplReplaceStr = [
            '__PUBLIC__' => "/{$themePath}public",
            ];
        $tplReplaceStr = array_merge ( config ( 'template.tpl_replace_string' ), $tplReplaceStr );
        config ( 'template.tpl_replace_string', $tplReplaceStr );
    }

    public function collect(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $user = json_decode(session('userInfo'),true);
            $issuer_type = isset($user['teacher_no']) ? 0 : 1;
            $param['user_type'] = $issuer_type;
            $param['uid'] = $user['id'];
            $insert = Db::name('collection')->insertGetId($param);
            if ($insert != false){
                return [
                    'code' => 1,
                    'msg' => '收藏成功',
                    'id' => $insert
                ];
            }else{
                return [
                    'code' => 0,
                    'msg' => '收藏失败'
                ];
            }

        }
    }

    public function unCollect(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $user = json_decode(session('userInfo'),true);
            $issuer_type = isset($user['teacher_no']) ? 0 : 1;
            $param['user_type'] = $issuer_type;
            $delete = Db::name('collection')->where('user_type',$issuer_type)->where('type',$param['type'])->where('cid',$param['cid'])->where('uid',$user['id'])->delete();
            if ($delete != false){
                return [
                    'code' => 1,
                    'msg' => '取消成功'
                ];
            }else{
                return [
                    'code' => 0,
                    'msg' => '取消失败'
                ];
            }
        }
    }
}
